package com.puman.watchtrade;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.puman.watchtrade.adapter.MyPagerAdapter;
import com.puman.watchtrade.util.Gjfun;
import com.puman.watchtrade.util.GlobalData;
import com.puman.watchtrade.util.LoadingProgressDialog;
import com.puman.watchtrade.util.MinPriorityThreadFactory;

public class IndexActivity extends FragmentActivity implements OnClickListener {
	/** Called when the activity is first created. */
	public static final int DELAY_TIME = 1000;
	public static final int DELAY_EXIT_TIME = 3000;
	public static final int BEGIN_TO = 0x01;
	public static final int NET_DISCON = 0x02;
	public static final int EXIT_APP = 0x3;
	public static final int LOADING = 0x4;

	private boolean isExit = false;

	private RelativeLayout layout;
	public static FragmentManager fragmentManager;
	static IndexActivity indexActivity;
	public ExecutorService cachedThreadPoolMinPriority = Executors
			.newCachedThreadPool(new MinPriorityThreadFactory()); // 线程池

	public static IndexActivity getInstant() {
		return indexActivity;
	}
	public static AlertDialog alertDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		indexActivity = this;
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		GlobalData.screenWidth = dm.widthPixels;
		GlobalData.screenHeight = dm.heightPixels;
		// JPushInterface.init(this);
		fragmentManager = getSupportFragmentManager();

		setContentView(R.layout.activity_index);
		// sendMesDelay(LOADING, 1000);
		sharedPreferences = getInstant().getSharedPreferences("watchtrade",
				Context.MODE_PRIVATE);
		editor = sharedPreferences.edit();// 获取编辑器
		initView();
		// sendMesDelay(BEGIN_TO, DELAY_TIME);
	}

	private LoadingProgressDialog mProgress = null;
	private Button welcomeIn;
	public ViewPager myViewPager;
	private List<View> listViews = new ArrayList<View>(); // Tab页面列表
	private int welcomeImgArr[] = { R.drawable.welcom, R.drawable.welcom1,
			R.drawable.welcom2, R.drawable.welcom3, R.drawable.welcom4,
			R.drawable.welcom5 };
	private SharedPreferences sharedPreferences; // 私有数据
	private Editor editor;

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	private void initView() {
		// sendMesDelay(LOADING, 1000);
		myViewPager = (ViewPager) this.findViewById(R.id.myviewpager);

		LayoutInflater mInflater = this.getLayoutInflater();

		if (sharedPreferences.getString("fristInApp", "").equals("1")) {
			sendMesDelay(LOADING, 700);
		} else {
			editor.putString("fristInApp", "1");// 1 代表已经不是第一次进入APP
			editor.commit();

			// listViews.add(mInflater.inflate(R.layout.index_show_layout,
			// null));
			listViews.add(mInflater.inflate(R.layout.index_show_layout1, null));
			listViews.add(mInflater.inflate(R.layout.index_show_layout2, null));
			listViews.add(mInflater.inflate(R.layout.index_show_layout3, null));
			listViews.add(mInflater.inflate(R.layout.index_show_layout4, null));
			listViews.add(mInflater.inflate(R.layout.index_show_layout5, null));

			listViews.get(4).findViewById(R.id.tohome)
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							mProgress = Gjfun.startProgressDialog(mProgress,
									getInstant());
							sendMesDelay(LOADING, 1000);
						}
					});
		}

		myViewPager.setAdapter(new MyPagerAdapter(listViews));
		myViewPager.setOnPageChangeListener(new MySetOnPageChangeListener());

	}

	/**
	 * ViewPager页面选项卡切换监听器
	 */
	class MySetOnPageChangeListener implements OnPageChangeListener {

		public void onPageScrollStateChanged(int arg0) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageSelected(int position) {
			// 设置透明度
			AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
			// 设置动画执行的时间
			alphaAnimation.setDuration(1000);

			// 从上网下 带透明
			AnimationSet animationSetFromeBelow = new AnimationSet(true);
			TranslateAnimation translateAnimationFromeBelow = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0f,// 参数1～2：x轴的开始位置
					Animation.RELATIVE_TO_SELF, 0f,// 参数5～6：x轴的结束位置
					Animation.RELATIVE_TO_SELF, 0.5f,// 参数3～4：y轴的开始位置
					Animation.RELATIVE_TO_SELF, 0f); // 参数7～8：y轴的结束位置

			translateAnimationFromeBelow.setDuration(1000);
			animationSetFromeBelow.addAnimation(alphaAnimation);
			animationSetFromeBelow.addAnimation(translateAnimationFromeBelow);

			// 从下往上 带透明
			AnimationSet animationSetFromeUp = new AnimationSet(true);
			TranslateAnimation translateAnimationFromeUp = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0f,// 参数1～2：x轴的开始位置
					Animation.RELATIVE_TO_SELF, 0f,// 参数5～6：x轴的结束位置
					Animation.RELATIVE_TO_SELF, -0.5f,// 参数3～4：y轴的开始位置
					Animation.RELATIVE_TO_SELF, 0f); // 参数7～8：y轴的结束位置
			translateAnimationFromeUp.setDuration(1000);
			animationSetFromeUp.addAnimation(alphaAnimation);
			animationSetFromeUp.addAnimation(translateAnimationFromeUp);

			// 从右往左 带透明
			AnimationSet animationSetFromeRight = new AnimationSet(true);
			TranslateAnimation translateAnimationFromeRight = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0.5f,// 参数1～2：x轴的开始位置
					Animation.RELATIVE_TO_SELF, 0f,// 参数5～6：x轴的结束位置
					Animation.RELATIVE_TO_SELF, 0f,// 参数3～4：y轴的开始位置
					Animation.RELATIVE_TO_SELF, 0f); // 参数7～8：y轴的结束位置
			translateAnimationFromeRight.setDuration(1000);
			animationSetFromeRight.addAnimation(alphaAnimation);
			animationSetFromeRight.addAnimation(translateAnimationFromeRight);

			// 从左右往右 带透明
			AnimationSet animationSetFromeLeft = new AnimationSet(true);
			TranslateAnimation translateAnimationFromeLeft = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, -0.5f,// 参数1～2：x轴的开始位置
					Animation.RELATIVE_TO_SELF, 0f,// 参数5～6：x轴的结束位置
					Animation.RELATIVE_TO_SELF, 0f,// 参数3～4：y轴的开始位置
					Animation.RELATIVE_TO_SELF, 0f); // 参数7～8：y轴的结束位置
			translateAnimationFromeLeft.setDuration(1000);
			animationSetFromeLeft.addAnimation(alphaAnimation);
			animationSetFromeLeft.addAnimation(translateAnimationFromeLeft);

			switch (position) {
			// case 0:
			//
			// break;
			case 0:
				listViews.get(0).findViewById(R.id.welcome_1_1)
						.startAnimation(animationSetFromeUp);
				listViews.get(0).findViewById(R.id.welcome_1_2)
						.startAnimation(animationSetFromeBelow);

				break;
			case 1:
				listViews.get(1).findViewById(R.id.welcome_2_1)
						.startAnimation(animationSetFromeUp);
				listViews.get(1).findViewById(R.id.welcome_2_2)
						.startAnimation(animationSetFromeBelow);
				break;
			case 2:
				listViews.get(2).findViewById(R.id.welcome_3_1)
						.startAnimation(animationSetFromeUp);
				listViews.get(2).findViewById(R.id.welcome_3_2)
						.startAnimation(animationSetFromeBelow);

				break;
			case 3:
				listViews.get(3).findViewById(R.id.welcome_4_1)
						.startAnimation(animationSetFromeRight);
				listViews.get(3).findViewById(R.id.welcome_4_2)
						.startAnimation(animationSetFromeLeft);
				break;
			case 4:
				AnimationSet animationSetFromeTop = new AnimationSet(true);
				TranslateAnimation translateAnimationFromeTop = new TranslateAnimation(
						Animation.RELATIVE_TO_SELF, 0f,// 参数1～2：x轴的开始位置
						Animation.RELATIVE_TO_SELF, 0f,// 参数5～6：x轴的结束位置
						Animation.RELATIVE_TO_SELF, -1.5f,// 参数3～4：y轴的开始位置
						Animation.RELATIVE_TO_SELF, 0f); // 参数7～8：y轴的结束位置
				translateAnimationFromeTop.setDuration(1500);
				animationSetFromeTop.addAnimation(alphaAnimation);
				animationSetFromeTop.addAnimation(translateAnimationFromeTop);
				listViews.get(4).findViewById(R.id.tohome)
						.startAnimation(animationSetFromeTop);
				break;

			default:
				break;
			}
		}
	}

	Callback callback = new Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case BEGIN_TO:
				Intent intent = new Intent();
				intent.setClass(IndexActivity.this, MainActivity.class);
				startActivity(intent);
				finish();//
				break;
			case NET_DISCON:
				System.exit(0);
				break;
			case EXIT_APP:
				isExit = false;
				break;
			case LOADING:
				Gjfun.stopProgressDialog(mProgress);
				jump();
				break;
			default:
				break;
			}
			return false;
		}
	};

	Handler handler = new Handler(callback);

	private void sendMesDelay(int what, int delay) {
		Message msg = new Message();
		msg.what = what;
		handler.sendMessageDelayed(msg, delay);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		default:
			break;
		}

	}

	private void jump() {
		Intent intent = new Intent();
		intent.setClass(IndexActivity.this, MainActivity.class);
		startActivity(intent);
		finish();//
	}

	public void exit() {
		if (!isExit) {
			isExit = true;
			Toast.makeText(getApplicationContext(), "再按一次退出程序...",
					Toast.LENGTH_SHORT).show();
			sendMesDelay(EXIT_APP, 1500);
		} else {
			Intent intent = new Intent(Intent.ACTION_MAIN); //
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
			System.exit(0);
		}
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
			return false;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		JPushInterface.onResume(indexActivity);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		JPushInterface.onPause(indexActivity);
	}

}
